##################################################################################################
#Ms: Why International Organizations Commit to Liberal Norms
#Authors: Tallberg, Lundgren, Sommerer, Squatrito
#Date: June 2020
#Software: R 3.6.3
#Hardware: Mac
##################################################################################################
# For coding and details, see list of variables in the online appendix


# Read required libraries 
# Use "install.libraries" function as required

library(ggplot2)
library(gridExtra)
library(lme4)
library(sjmisc)
library(sjPlot)
library(stargazer)

# Read data (set source directory to where data files are saved)

data<-read.csv("~/Dropbox/tallbergetal_data.csv") 

# Figure 1 

norms<-unique(as.character(data$NormID))

for (i in norms){
  tempdata<-data[data$NormID==i,]
  
  test<-aggregate(year~ioname,data=data,FUN=mean)
  test2<-aggregate(year~ioname,data=tempdata[tempdata$term_surv==1,],FUN=sum) 
  
  test<-merge(test,test2,by="ioname",all.x=T)
  
  test[,2]<-NULL
  colnames(test)[2]<-"year"
  test$level<-"level1"
  test <- test[order(test$year),] 
  test4<-aggregate(year~ioname,data=data,FUN=mean)
  test3<-aggregate(year~ioname,data=tempdata[tempdata$main_surv==1,],FUN=sum)
  test3<-merge(test4,test3,by="ioname",all.x=T)
  test3[,2]<-NULL
  colnames(test3)[2]<-"year"
  test3$level<-"level3"
  test<-rbind(test,test3)
  test<-merge(test,test3[,1:2],by="ioname",all.x=T)
  colnames(test)[2]<-"year"
  colnames(test)[3]<-"Level"
  colnames(test)[4]<-"year_1"
  test$Level[test$Level=="level1"]<-"Norm recognition"
  test$Level[test$Level=="level3"]<-"Norm commitment"
  test$year_1[is.na(test$year_1)]<-test$year[is.na(test$year_1)]
  test$year_1[test$Level=="Norm commitment"]<- test$year_1[test$Level=="Norm commitment"]-50
  for (j in unique(test$ioname)){
    ifelse(is.na(test$year_1[test$ioname==j & test$Level=="Norm commitment"]),test$year_1[test$ioname==j & test$Level=="Norm commitment"]<-test$year_1[test$ioname==j & test$Level=="Norm recognition"],NA)
  }
  
  gg<- ggplot(test, aes(y=reorder(ioname, year_1,min), x=year)) + geom_point(size=3, aes(pch=Level)) +coord_cartesian(xlim = c(1980, 2015))+scale_shape_manual(values=c(16,1)) 
  gg<- gg + geom_line(color="grey",size=.4)
  gg<- gg + theme_bw() + theme(panel.grid.major.x = element_blank(),
                               panel.grid.minor.x = element_blank())+
    theme(axis.text.y=element_text(size=rel(1)))+   labs(y=" ", x="Year")  
  gg<-gg+theme(axis.title=element_text(size=10))
  gg<-gg + theme(legend.position="none")
  gg<-gg+theme(panel.grid.major.y = element_line(size=.05, colour = "grey"))
  gg<-gg+ggtitle(i)
  assign(paste0("gg", i),gg)
  
}

ggDP<-ggDP+ggtitle("Democracy promotion")
ggDR<-ggDR+ggtitle("Debt relief")
ggGE<-ggGE+ggtitle("Gender equality")
ggGG<-ggGG+ggtitle("Good governance")
ggHS<-ggHS+ggtitle("Human security")
ggR2P<-ggR2P+ggtitle("Responsibility to protect")
ggSD<-ggSD+ggtitle("Sustainable development")
ggDER<-ggDER+ggtitle("Deregulation")

grid.arrange(ggDER,ggDP,ggDR,ggGE,ggGG,ggHS,ggR2P,ggSD,nrow=4)

# Figure 2 

temp<-aggregate(data$demdensity~data$ioname,data=data,FUN=mean)
colnames(temp)[1]<-"ioname"
temp$iocat<-c()
temp$iocat[temp$ioname %in% c("EU","NC","OECD","COE")]<-"High"
temp$iocat[temp$ioname %in% c("OSCE","CAN","CARICOM","PIF","OAS","APEC","COMW","UN")]<-"Medium"
temp$iocat[temp$ioname %in% c("AMU","SADC","SCO","OIC","AU","ASEAN")]<-"Low"
temp[,2]<-NULL
data$iocat<-NULL
data<-merge(data,temp,by="ioname",all.x=T)
head(data)
data$main_surv_plot<-data$main_surv
data$main_surv_plot[data$main_surv==0 & data$year ==2015]<-1
storage<-c()
categories<-unique(temp$iocat)
for (i in categories){
  tempdata<-data[data$iocat==i,]
  test<-aggregate(year~NormID,data=data,FUN=mean)
  test2<-aggregate(year~NormID,data=tempdata[tempdata$main_surv_plot==1,],FUN=mean) 
  test<-merge(test,test2,by="NormID",all.x=T)
  test[,2]<-NULL
  colnames(test)[2]<-"year"
  test$cat<-i
  test$year[test$year==2015]<-NA
  storage<-rbind(storage, test)
}

storage$order<-storage$year
for (j in unique(storage$NormID)){
  storage$order[storage$NormID==j]<-min(storage$order[storage$NormID==j],na.rm=T)
}

categories<-unique(temp$iocat)

for (i in categories){
  tempdata<-data[data$iocat==i,]
  test<-aggregate(year~NormID,data=data,FUN=mean)
  test2<-aggregate(year~NormID,data=tempdata[tempdata$main_surv_plot==1,],FUN=mean) 
  test<-merge(test,test2,by="NormID",all.x=T)
  test[,2]<-NULL
  colnames(test)[2]<-"year"
  test$year[test$year==2015]<-NA
  test$year_1[test$NormID=="SD"]<-3
  test$year_1[test$NormID=="GE"]<-1
  test$year_1[test$NormID=="GG"]<-5
  test$year_1[test$NormID=="DP"]<-4
  test$year_1[test$NormID=="R2P"]<-6
  test$year_1[test$NormID=="HS"]<-7
  test$year_1[test$NormID=="DR"]<-8
  test$year_1[test$NormID=="DER"]<-2
  
  gg<- ggplot(test, aes(y=reorder(NormID, year_1), x=year)) + geom_point(size=3) +coord_cartesian(xlim = c(1980, 2015))+scale_shape_manual(values=c(1,16)) 
  gg<- gg + theme_bw()
  gg<- gg + theme(axis.text.y=element_text(size=rel(1)))+   labs(y=" ", x="Year") 
  gg<-gg + theme(legend.position="none")
  gg<-gg+theme(axis.title=element_text(size=10))
  gg<-gg+ggtitle(i)
  gg<- gg + theme(panel.grid.major.y = element_line(colour="grey", size=0.3))
  
  assign(paste0("gg", i),gg)
  
}

grid.arrange(ggHigh,ggMedium,ggLow,nrow=1) + theme(panel.grid.major.x = element_blank())


# Figure 3 (3x3 plots)

temp<-aggregate(data$pooling~data$ioname,data=data,FUN=mean)
colnames(temp)[1]<-"ioname"
temp$iocat<-c()
temp$iocat[temp$ioname %in% c("AU","CARICOM","COE","COMW","OAS","OIC","UN")]<-"High"
temp$iocat[temp$ioname %in% c("ASEAN","CAN","EU","OSCE","PIF","SADC","SCO")]<-"Medium"
temp$iocat[temp$ioname %in% c("AMU","APEC","NC","OECD")]<-"Low"
temp[,2]<-NULL
data$iocat<-NULL
data<-merge(data,temp,by="ioname",all.x=T)
categories<-unique(temp$iocat)
data$main_surv_plot<-data$main_surv
data$main_surv_plot[data$main_surv==0 & data$year ==2015]<-1

for (i in categories){
  tempdata<-data[data$iocat==i,]
  test<-aggregate(year~NormID,data=data,FUN=mean)
  test2<-aggregate(year~NormID,data=tempdata[tempdata$main_surv_plot==1,],FUN=mean) 
  test<-merge(test,test2,by="NormID",all.x=T)
  
  test[,2]<-NULL
  colnames(test)[2]<-"year"
  test$year[test$year==2015]<-NA
  test$year_1[test$NormID=="SD"]<-3
  test$year_1[test$NormID=="GE"]<-2
  test$year_1[test$NormID=="GG"]<-4
  test$year_1[test$NormID=="DP"]<-1
  test$year_1[test$NormID=="R2P"]<-6
  test$year_1[test$NormID=="HS"]<-7
  test$year_1[test$NormID=="DR"]<-8
  test$year_1[test$NormID=="DER"]<-5
  
  
  gg<- ggplot(test, aes(y=reorder(NormID, year_1), x=year)) + geom_point(size=3) +coord_cartesian(xlim = c(1980, 2015))+scale_shape_manual(values=c(1,16)) 
  gg<- gg + theme_bw()
  gg<- gg + theme(axis.text.y=element_text(size=rel(1)))+   labs(y=" ", x="Year") 
  gg<-gg + theme(legend.position="none")
  gg<-gg+theme(axis.title=element_text(size=10))
  gg<-gg+ggtitle(i)
  gg<- gg + theme(panel.grid.major.y = element_line(colour="grey", size=0.3))
  assign(paste0("gg", i),gg)
  
}

grid.arrange(ggHigh,ggMedium,ggLow,nrow=1)


temp<-aggregate(data$delegation~data$ioname,data=data,FUN=mean)
colnames(temp)[1]<-"ioname"
temp$iocat<-c()
temp$iocat[temp$ioname %in% c("AU","CAN", "CARICOM","COE","COMW","EU","PIF","SADC")]<-"High"
temp$iocat[temp$ioname %in% c("ASEAN","NC","OAS","OECD","OSCE","SCO","UN")]<-"Medium"
temp$iocat[temp$ioname %in% c("AMU","APEC","OIC")]<-"Low"
temp[,2]<-NULL
data$iocat<-NULL
data<-merge(data,temp,by="ioname",all.x=T)
categories<-unique(temp$iocat)
data$main_surv_plot<-data$main_surv
data$main_surv_plot[data$main_surv==0 & data$year ==2015]<-1

for (i in categories){
  tempdata<-data[data$iocat==i,]
  test<-aggregate(year~NormID,data=data,FUN=mean)
  test2<-aggregate(year~NormID,data=tempdata[tempdata$main_surv_plot==1,],FUN=mean) 
  test<-merge(test,test2,by="NormID",all.x=T)
  test[,2]<-NULL
  colnames(test)[2]<-"year"
  test$year[test$year==2015]<-NA
  test$year_1[test$NormID=="SD"]<-3
  test$year_1[test$NormID=="GE"]<-2
  test$year_1[test$NormID=="GG"]<-4
  test$year_1[test$NormID=="DP"]<-1
  test$year_1[test$NormID=="R2P"]<-6
  test$year_1[test$NormID=="HS"]<-7
  test$year_1[test$NormID=="DR"]<-8
  test$year_1[test$NormID=="DER"]<-5
  
  gg<- ggplot(test, aes(y=reorder(NormID, year_1), x=year)) + geom_point(size=3) +coord_cartesian(xlim = c(1980, 2015))+scale_shape_manual(values=c(1,16)) 
  gg<- gg + theme_bw()
  gg<- gg + theme(axis.text.y=element_text(size=rel(1)))+   labs(y=" ", x="Year") 
  gg<-gg + theme(legend.position="none")
  gg<-gg+theme(axis.title=element_text(size=10))
  gg<-gg+ggtitle(i)
  gg<- gg + theme(panel.grid.major.y = element_line(colour="grey", size=0.3))
  assign(paste0("gg", i),gg)
  
}

grid.arrange(ggHigh,ggMedium,ggLow,nrow=1)


temp<-aggregate(data$delegation~data$ioname,data=data,FUN=mean)
colnames(temp)[1]<-"ioname"
temp$iocat<-c()
temp$iocat[temp$ioname %in% c("AU","CAN","COE","EU","OAS", "OSCE","UN")]<-"High"
temp$iocat[temp$ioname %in% c("APEC","ASEAN","CARICOM","COMW", "NC","OECD","PIF","SADC")]<-"Medium"
temp$iocat[temp$ioname %in% c("AMU","OIC","SCO")]<-"Low"
temp[,2]<-NULL
data$iocat<-NULL
data<-merge(data,temp,by="ioname",all.x=T)

categories<-unique(temp$iocat)
data$main_surv_plot<-data$main_surv
data$main_surv_plot[data$main_surv==0 & data$year ==2015]<-1
for (i in categories){
  tempdata<-data[data$iocat==i,]
  test<-aggregate(year~NormID,data=data,FUN=mean)
  test2<-aggregate(year~NormID,data=tempdata[tempdata$main_surv_plot==1,],FUN=mean) 
  test<-merge(test,test2,by="NormID",all.x=T)
  test[,2]<-NULL
  colnames(test)[2]<-"year"
  test$year[test$year==2015]<-NA
  test$year_1[test$NormID=="SD"]<-3
  test$year_1[test$NormID=="GE"]<-1
  test$year_1[test$NormID=="GG"]<-5
  test$year_1[test$NormID=="DP"]<-2
  test$year_1[test$NormID=="R2P"]<-6
  test$year_1[test$NormID=="HS"]<-7
  test$year_1[test$NormID=="DR"]<-8
  test$year_1[test$NormID=="DER"]<-4
  
  gg<- ggplot(test, aes(y=reorder(NormID, year_1), x=year)) + geom_point(size=3) +coord_cartesian(xlim = c(1980, 2015))+scale_shape_manual(values=c(1,16)) 
  gg<- gg + theme_bw()
  gg<- gg + theme(axis.text.y=element_text(size=rel(1)))+   labs(y=" ", x="Year") 
  gg<-gg + theme(legend.position="none")
  gg<-gg+theme(axis.title=element_text(size=10))
  gg<-gg+ggtitle(i)
  gg<- gg + theme(panel.grid.major.y = element_line(colour="grey", size=0.3))
  assign(paste0("gg", i),gg)
  
}

grid.arrange(ggHigh,ggMedium,ggLow,nrow=1)



# Table 1

model1 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model2 <- glmer(main_surv ~  l1_SLglobal_main_scaled + Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model3 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model4 <- glmer(main_surv ~  l1_SLglobal_main_scaled + Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model5 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled + delegation_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model6 <- glmer(main_surv ~  l1_SLglobal_main_scaled + Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled + delegation_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

stargazer(model1,model2,model3,model4,model5,model6, type="text",digits=2,df=F,star.cutoffs = c(0.05, 0.01))


# Table 2

model7 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model8 <- glmer(term_surv ~  l1_SLglobal_term_scaled + Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model9 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model10 <- glmer(term_surv ~  l1_SLglobal_term_scaled + Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model11 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled + delegation_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model12 <- glmer(term_surv ~  l1_SLglobal_term_scaled + Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled + delegation_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

stargazer(model7,model8,model9,model10,model11,model12, type="text",digits=2,df=F,star.cutoffs = c(0.05, 0.01))


# Figure 4

set_theme(base = theme_classic(),axis.title.size=0.9,axis.textsize=.9)

p1<-plot_model(model1,type="pred",terms="demdensity_scaled",title="",axis.title=c("Democratic density","Annual probability of norm commitment"),ci.lvl=.95,xlim(-1,1))
p1<-p1+xlim(-1,1.1)
p1<-p1+ggtitle("(a)")
p1<-p1+ylim(0,0.05)
p1<-p1+ coord_cartesian(ylim = c(0.00235, 0.05))

p2<-plot_model(model1,type="pred",terms="l1_SLmain_region_scaled",title="",axis.title=c("Norm prominence in field (region)","Annual probability of norm commitment"),ci.lvl=.95,xlim(-1,1))
p2<-p2+ggtitle("(b)")
p2<-p2+xlim(-.2,1.8)
p2<-p2+ylim(0,.05)
p2<-p2+ coord_cartesian(ylim = c(.00235, .05))

p3<-plot_model(model7,type="pred",terms="demdensity_scaled",title="",axis.title=c("Democratic density","Annual probability of norm recognition"),ci.lvl=.95,xlim(-1,1))
p3<-p3+ggtitle("(c)")
p3<-p3+xlim(-1,1)
p3<-p3+ylim(0,.12)
p3<-p3+ coord_cartesian(ylim = c(.0057, .12))

p4<-plot_model(model7,type="pred",terms="l1_SLterm_region_scaled",title="",axis.title=c("Norm prominence in field (region)","Annual probability of norm recognition"),ci.lvl=.95,xlim(-1,1), main="(b)")
p4<-p4+ggtitle("(d)")
p4<-p4+xlim(-1.1,1.1)
p4<-p4+ylim(0,.12)
p4<-p4+ coord_cartesian(ylim = c(.0057, .12))

grid.arrange(p1,p2,p3,p4,nrow=2)


# ONLINE APPENDIX

# Figure A2 in online appendix 
# Please note that plots for AMU and SCO are produced separately before presenting all plots with the grid.arrange command

ios<-unique(as.character(data$ioname))
ios<-ios[ios!="AMU"]
ios<-ios[ios!="SCO"]

for (i in ios){
  tempdata<-data[data$ioname==i,]
  test<-aggregate(year~NormID,data=data,FUN=mean)
  test2<-aggregate(year~NormID,data=tempdata[tempdata$term_surv==1,],FUN=sum) 
  test<-merge(test,test2,by="NormID",all.x=T)
  test[,2]<-NULL
  colnames(test)[2]<-"year"
  test$level<-"level1"
  test <- test[order(test$year),] 
  test4<-aggregate(year~NormID,data=data,FUN=mean)
  test3<-aggregate(year~NormID,data=tempdata[tempdata$main_surv==1,],FUN=sum)
  test3<-merge(test4,test3,by="NormID",all.x=T)
  test3[,2]<-NULL
  colnames(test3)[2]<-"year"
  test3$level<-"level3"
  test<-rbind(test,test3)
  test<-merge(test,test3[,1:2],by="NormID",all.x=T)
  colnames(test)[2]<-"year"
  colnames(test)[3]<-"Level"
  colnames(test)[4]<-"year_1"
  test$Level[test$Level=="level1"]<-"Norm recognition"
  test$Level[test$Level=="level3"]<-"Norm commitment"
  test$year_1[is.na(test$year_1)]<-test$year[is.na(test$year_1)]
  test$year_1[test$Level=="Norm commitment"]<- test$year_1[test$Level=="Norm commitment"]-50
  
  for (j in unique(test$NormID)){
    ifelse(is.na(test$year_1[test$NormID==j & test$Level=="Norm commitment"]),test$year_1[test$NormID==j & test$Level=="Norm commitment"]<-test$year_1[test$NormID==j & test$Level=="Norm recognition"],NA)
  }
  
  gg<- ggplot(test, aes(y=reorder(NormID, year_1,min), x=year)) + geom_point(size=3, aes(pch=Level)) +coord_cartesian(xlim = c(1980, 2015))+scale_shape_manual(values=c(16,1)) 
  gg<- gg + geom_line(color="grey",size=.4)
  gg<- gg + theme_bw() + theme(panel.grid.major.x = element_blank(),
                               panel.grid.minor.x = element_blank())+
    theme(axis.text.y=element_text(size=rel(1)))+   labs(y=" ", x="Year")  
  gg<-gg+theme(axis.title=element_text(size=10))
  gg<-gg + theme(legend.position="none")
  gg<-gg+theme(panel.grid.major.y = element_line(size=.05, colour = "grey"))
  gg<-gg+ggtitle(i)
  assign(paste0("gg", i),gg)
  
}

tempdata<-data[data$ioname=="SCO",]
head(tempdata)
test<-aggregate(year~NormID,data=data,FUN=mean)
test2<-aggregate(year~NormID,data=tempdata[tempdata$term_surv==1,],FUN=sum) 
test<-merge(test,test2,by="NormID",all.x=T)
test[,2]<-NULL
colnames(test)[2]<-"year"
test$level<-"level1"
test <- test[order(test$year),] 
test$year_1<-test$year
colnames(test)[2]<-"year"
colnames(test)[3]<-"Adoption"
colnames(test)[4]<-"year_1"
test$Adoption[test$Adoption=="level1"]<-"First reference"
test$Adoption[test$Adoption=="level3"]<-"Full policy"
gg<- ggplot(test, aes(y=reorder(NormID, year_1,min), x=year)) + geom_point(size=3, aes(pch=Adoption)) +coord_cartesian(xlim = c(1980, 2015))+scale_shape_manual(values=c(1,16)) 
gg<- gg + geom_line(color="grey",size=.4)
gg<- gg + theme_bw() + theme(panel.grid.major.x = element_blank(),
                             panel.grid.minor.x = element_blank())+
  theme(axis.text.y=element_text(size=rel(1)))+   labs(y=" ", x="Year")  +theme(panel.grid.major.y = element_line(size=.2))
gg<-gg + theme(legend.position="none")
gg<-gg+theme(axis.title=element_text(size=10))
gg<-gg+ggtitle("SCO")
ggSCO<-gg

data_AMU<-data[data$ioname=="AMU",]
test<-aggregate(year~NormID,data=data,FUN=mean)
test$year<-0
ggAMU <- ggplot(test, aes(y=reorder(NormID, year), x=year)) +coord_cartesian(xlim = c(1980, 2015))+scale_shape_manual(values=c(1, 16)) 
ggAMU<- ggAMU + geom_line(color="steelblue",size=.3)
ggAMU<- ggAMU + theme_bw() + theme(panel.grid.major.x = element_blank(),
                                   panel.grid.minor.x = element_blank())+
  theme(axis.text.y=element_text(size=rel(1)))+   labs(y=" ", x="Year") 
ggAMU<-ggAMU + theme(legend.position="none")
ggAMU<-ggAMU+theme(axis.title=element_text(size=10))
ggAMU<-ggAMU+ggtitle("AMU")

grid.arrange(ggAMU,ggAPEC,ggASEAN,ggAU, ggCAN,ggCARICOM,ggCOMW,ggCOE,ggEU,ggNC,ggOAS,ggOECD,ggOIC, ggOSCE,ggPIF,ggSADC,ggSCO,ggUN,nrow=6)




# Table A6: Region dummies

model1 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+afri+asia+amer+europ+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model2 <- glmer(main_surv ~  l1_SLglobal_main_scaled + Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+afri+asia+amer+europ+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model3 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+afri+asia+amer+europ+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model4 <- glmer(term_surv ~  l1_SLglobal_term_scaled + Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+afri+asia+amer+europ+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

stargazer(model1,model2,model3,model4, type="text",digits=2,df=F,star.cutoffs = c(0.05, 0.01))



# # Table A7: Additional controls

model1 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+ Global+resource_cat_scaled+time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model2 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+ relevance+resource_cat_scaled+time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model3 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+ Pol_vol +resource_cat_scaled+time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model4 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+ Global+resource_cat_scaled+time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model5 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+ relevance+resource_cat_scaled+time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model6 <- glmer(term_surv ~  l1_SLterm_region_scaled +Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+ Pol_vol +resource_cat_scaled+time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

stargazer(model1,model2,model3,model4,model5,model6, type="text",digits=2,star.cutoffs = c(0.05, 0.01))
stargazer(model1,model2,model3,model4,model5,model6, type="html",digits=2,star.cutoffs = c(0.05, 0.01))



# Table A.8. Alternative operationalizations of democracy + KOF globalization index

model1 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +DD_share+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled  +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model2 <- glmer(main_surv ~  l1_SLglobal_main_scaled + Ngram_imp_log_scaled +DD_share+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled  +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model3 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +DD_share+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled  +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model4 <- glmer(term_surv ~  l1_SLglobal_term_scaled + Ngram_imp_log_scaled +DD_share+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled  +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model5 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +KOFPoGIdj+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled  +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model6 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +KOFPoGIdj+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled  +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

stargazer(model1,model2,model3,model4,model5,model6, type="text",digits=2,df=F, star.cutoffs = c(0.05, 0.01))


# Table A9: Sub-samples (IOs and norms)

data_5norms<-data[data$NormID=="SD"|data$NormID=="GG"|data$NormID=="GE"|data$NormID=="DER"|data$NormID=="DP",]
data$autoc_IOs[data$ioname=="AU"]<-0

model1 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data_5norms,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model2 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data[data$autoc_IOs==0,],control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model3 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data_5norms,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model4 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data[data$autoc_IOs==0,],control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

stargazer(model1,model2,model3,model4, type="text",digits=2,df=F,star.cutoffs = c(0.05, 0.01))



# Figure A3 

set_theme(base = theme_classic(),axis.title.size=0.9,axis.textsize=.9)

model2 <- glmer(main_surv ~  l1_SLglobal_main_scaled + Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

model3 <- glmer(main_surv ~  l1_SLmain_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))


p5<-plot_model(model2,type="pred",terms="l1_SLglobal_main_scaled",title="",axis.title=c("Norm prominence in field (global)","Annual probability of norm commitment"),ci.lvl=.95,xlim(-1,1))
p5<-p5+ coord_cartesian(ylim = c(0.00125, .027),xlim=c(-1,1.1))
p5

p6<-plot_model(model2,type="pred",terms="pooling_scaled",title="",axis.title=c("Pooling","Annual probability of norm commitment"),ci.lvl=.95,xlim(-1,1))
p6<-p6+ coord_cartesian(ylim = c(0.0014, .03),xlim=c(-1,1))
p6

p7<-plot_model(model2,type="pred",terms="delegation_scaled",title="",axis.title=c("Delegation","Annual probability of norm commitment"),ci.lvl=.95)
p7<-p7+ coord_cartesian(ylim = c(.0014, .03),xlim = c(-1,1))
p7

p8<-plot_model(model3,type="pred",terms="tna_access_scaled",title="",axis.title=c("TNA access","Annual probability of norm commitment"),ci.lvl=.95,xlim(-1,1))
p8<-p8+ coord_cartesian(xlim=c(-1,1),ylim = c(.0014, .03))
p8

grid.arrange(p5,p6,p7,p8,nrow=2)


# Figure A4

model1 <- glmer(term_surv ~  l1_SLterm_region_scaled+ Ngram_imp_log_scaled +demdensity_scaled+ pooling_scaled+ delegation_scaled + tna_access_scaled + Memb_scaled + demdensity_sd_scaled +dempowMS+resource_cat_scaled +time+ I(time^2)+(1  | NormID) +  (1 |ioname)+ (1 |year),family=binomial(link='logit'),data=data,control = glmerControl(optimizer = "bobyqa",calc.derivs = F,optCtrl=list(maxfun=1e5)))

p9<-plot_model(model1,type="pred",terms="Ngram_imp_log_scaled",title="",axis.title=c("Norm prominence in society","Annual probability of norm recognition"),ci.lvl=.95,xlim(-1,1))
p9<-p9+ coord_cartesian(xlim=c(-1,1),ylim = c(.0056, .12))
p9

p10<-plot_model(model1,type="pred",terms="delegation_scaled",title="",axis.title=c("Delegation","Annual probability of norm recognition"),ci.lvl=.95,xlim(-1,1))
p10<-p10+ coord_cartesian(xlim=c(-1,1),ylim = c(.0056, .12))
p10

grid.arrange(p9,p10,nrow=1)






